package cn.iocoder.yudao.module.system.controller.admin.partnercouponshare;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.partnercouponshare.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.partnercouponshare.PartnerCouponShareDO;
import cn.iocoder.yudao.module.system.service.partnercouponshare.PartnerCouponShareService;

@Tag(name = "管理后台 - 合伙人优惠券分享记录")
@RestController
@RequestMapping("/system/partner-coupon-share")
@Validated
public class PartnerCouponShareController {

    @Resource
    private PartnerCouponShareService partnerCouponShareService;

    @PostMapping("/create")
    @Operation(summary = "创建合伙人优惠券分享记录")
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:create')")
    public CommonResult<Long> createPartnerCouponShare(@Valid @RequestBody PartnerCouponShareSaveReqVO createReqVO) {
        return success(partnerCouponShareService.createPartnerCouponShare(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合伙人优惠券分享记录")
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:update')")
    public CommonResult<Boolean> updatePartnerCouponShare(@Valid @RequestBody PartnerCouponShareSaveReqVO updateReqVO) {
        partnerCouponShareService.updatePartnerCouponShare(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合伙人优惠券分享记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:delete')")
    public CommonResult<Boolean> deletePartnerCouponShare(@RequestParam("id") Long id) {
        partnerCouponShareService.deletePartnerCouponShare(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合伙人优惠券分享记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:query')")
    public CommonResult<PartnerCouponShareRespVO> getPartnerCouponShare(@RequestParam("id") Long id) {
        PartnerCouponShareDO partnerCouponShare = partnerCouponShareService.getPartnerCouponShare(id);
        return success(BeanUtils.toBean(partnerCouponShare, PartnerCouponShareRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合伙人优惠券分享记录分页")
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:query')")
    public CommonResult<PageResult<PartnerCouponShareRespVO>> getPartnerCouponSharePage(@Valid PartnerCouponSharePageReqVO pageReqVO) {
        PageResult<PartnerCouponShareDO> pageResult = partnerCouponShareService.getPartnerCouponSharePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartnerCouponShareRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合伙人优惠券分享记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:partner-coupon-share:export')")
    @OperateLog(type = EXPORT)
    public void exportPartnerCouponShareExcel(@Valid PartnerCouponSharePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartnerCouponShareDO> list = partnerCouponShareService.getPartnerCouponSharePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合伙人优惠券分享记录.xls", "数据", PartnerCouponShareRespVO.class,
                        BeanUtils.toBean(list, PartnerCouponShareRespVO.class));
    }

}